<?php

namespace app\models;

use yii\data\Pagination;
use yii\data\ArrayDataProvider;
use yii\behaviors\TimestampBehavior;

class Field extends ActiveRecord
{
    const MODULE_CUSTOMER = 1;
    const MODULE_PRODUCT = 2;
    const MODULE_ORDER = 3;

    public static function getModuleMapping()
    {
        return [
            ['id' => self::MODULE_CUSTOMER, 'name' => '客户'],
            ['id' => self::MODULE_PRODUCT, 'name' => '产品'],
            ['id' => self::MODULE_ORDER, 'name' => '客户'],
        ];
    }

    const TYPE_MAIN = 1;
    const TYPE_ADDITIONAL = 2;

    public static function getTypeMapping()
    {
        return [
            ['id' => self::TYPE_MAIN, 'name' => '主要信息（可用于搜索）'],
            ['id' => self::TYPE_ADDITIONAL, 'name' => '附加信息'],
        ];
    }

    const VALIDATE_YES = 1;
    const VALIDATE_NO = 0;

    public static function getValidateMapping()
    {
        return [
            ['id' => self::VALIDATE_YES, 'name' => '是'],
            ['id' => self::VALIDATE_NO, 'name' => '否'],
        ];
    }

    const UNIQUE_YES = 1;
    const UNIQUE_NO = 0;

    public static function getUniqueMapping()
    {
        return [
            ['id' => self::UNIQUE_YES, 'name' => '是'],
            ['id' => self::UNIQUE_NO, 'name' => '是'],
        ];
    }

    const CHECK_YES = 1;
    const CHECK_NO = 0;

    public static function getCheckMapping()
    {
        return [
            ['id' => self::CHECK_YES, 'name' => '是'],
            ['id' => self::CHECK_NO, 'name' => '否'],
        ];
    }

    const ALLOW_EMPTY_YES = 1;
    const ALLOW_EMPTY_NO = 0;

    public static function getAllowEmptyMapping()
    {
        return [
            ['id' => self::ALLOW_EMPTY_YES, 'name' => '是'],
            ['id' => self::ALLOW_EMPTY_NO, 'name' => '否'],
        ];
    }

    const LIST_SHOW_YES = 1;
    const LIST_SHOW_NO = 0;

    public static function getListShowMapping()
    {
        return [
            ['id' => self::LIST_SHOW_YES, 'name' => '是'],
            ['id' => self::LIST_SHOW_NO, 'name' => '否'],
        ];
    }

    const ADD_SHOW_YES = 1;
    const ADD_SHOW_NO = 0;

    public static function getAddShowMapping()
    {
        return [
            ['id' => self::ADD_SHOW_YES, 'name' => '是'],
            ['id' => self::ADD_SHOW_NO, 'name' => '否'],
        ];
    }

    const FORM_TYPE_INPUT = 1;
    const FORM_TYPE_AREA = 2;
    const FORM_TYPE_SELECT = 3;
    const FORM_TYPE_DATE = 4;
    const FORM_TYPE_NUM = 5;
    const FORM_TYPE_DOUBLE = 6;
    const FORM_TYPE_PHONE = 7;
    const FORM_TYPE_TELEPHONE = 8;
    const FORM_TYPE_EMAIL = 9;
    const FORM_TYPE_ADDRESS = 10;

    public static function getFormTypeMapping()
    {
        return [
            ['id' => self::FORM_TYPE_INPUT, 'name' => '单行文本'],
            ['id' => self::FORM_TYPE_AREA, 'name' => '多行文本'],
            ['id' => self::FORM_TYPE_SELECT, 'name' => '选项'],
            ['id' => self::FORM_TYPE_DATE, 'name' => '日期'],
            ['id' => self::FORM_TYPE_NUM, 'name' => '数字'],
            ['id' => self::FORM_TYPE_DOUBLE, 'name' => '小数（小数点两位）'],
            ['id' => self::FORM_TYPE_PHONE, 'name' => '手机'],
            ['id' => self::FORM_TYPE_TELEPHONE, 'name' => '电话'],
            ['id' => self::FORM_TYPE_EMAIL, 'name' => '邮箱'],
            ['id' => self::FORM_TYPE_ADDRESS, 'name' => '地址'],
        ];
    }

    const AUTH_READ_AND_WRITE = 1;
    const AUTH_READ = 2;
    const AUTH_READ_MD5 = 3;
    const AUTH_INVISIBLE = 4;

    public static function getAuthMapping()
    {
        return [
            ['id' => self::AUTH_READ_AND_WRITE, 'name' => '读写'],
            ['id' => self::AUTH_READ, 'name' => '只读'],
            ['id' => self::AUTH_READ_MD5, 'name' => '只读密文'],
            ['id' => self::AUTH_INVISIBLE, 'name' => '不可见']
        ];
    }

    public static function tableName()
    {
        return '{{%field}}';
    }

    public function rules()
    {
        return [
            [
                [
                    'module',
                    'type',
                    'form_type',
                    'name',
                    'max_length',
                    'default_value',
                    'value',
                    'tips',
                    'is_validate',
                    'is_unique',
                    'is_check',
                    'is_allow_empty',
                    'in_list_show',
                    'in_add_show',
                    'status',
                    'auth',
                    'sort'
                ],
                'safe'
            ]
        ];
    }

    public function behaviors()
    {
        return [['class' => TimestampBehavior::class]];
    }

    public static function getListExt($param)
    {
        $query = self::find();
        $query->andFilterWhere(['module' => $param['module']]);

        $pagination = new Pagination([
            'defaultPageSize' => 1,
            'totalCount' => $query->count()
        ]);

        $pagination->setPage($param['page'] - 1);
        $pagination->setPageSize($param['pageSize']);

        $data = $query->offset($pagination->getOffset())
            ->orderBy('id DESC')
            ->limit($pagination->getLimit())->all();

        $dataProvider = new ArrayDataProvider([
            'models' => $data,
            'Pagination' => $pagination,
        ]);

        $formTypeList = Field::getFormTypeMapping();
        $formTypeList = array_combine(array_column($formTypeList, 'id'), array_column($formTypeList, 'name'));
        $allowEmptyList = Field::getAllowEmptyMapping();
        $allowEmptyList = array_combine(array_column($allowEmptyList, 'id'), array_column($allowEmptyList, 'name'));

        $field = [];
        $models = $dataProvider->getModels();
        foreach ($models as $arr) {
            $field[] = [
                'id' => $arr['id'],
                'name' => $arr['name'],
                'type' => $formTypeList[$arr['type']],
                'status' => $arr['status'],
                'auth' => $arr['auth'],
                'allowEmpty' => $allowEmptyList[$arr['is_allow_empty']],
            ];
        }

        $pageSize = $pagination->getPageSize();
        $totalPage = $pagination->getPageCount();
        $totalCount = $pagination->totalCount;

        return [
            'field' => $field,
            'totalCount' => (int)$totalCount,
            'pageSize' => $pageSize,
            'totalPage' => $totalPage,
        ];
    }
}